package com.sailfish.springbootdemo.dao.db4;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.common.DB4TableName;
import com.sailfish.springbootdemo.pojo.db4.EmailReminder;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface EmailReminderDao {

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB4TableName.EMAIL_REMINDER + " " +
            "where 1=1 " +
            "<if test='status != null'> " +
            " AND status = #{status} " +
            "</if> " +
            " ORDER BY create_time DESC " +
            "</script>"
    )
    List<EmailReminder> getEmailReminderByStatus(@Param("status") Integer status);

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB4TableName.EMAIL_REMINDER + " " +
            "where 1=1 " +
            "<if test='mail_type != null'> " +
            " AND mail_type = #{mail_type} " +
            "</if> " +
            "<if test='correlation_id != null'> " +
            " AND correlation_id = #{correlation_id} " +
            "</if> " +
            "<if test='create_time != null'> " +
            " AND DATE(create_time) = #{create_time} " +
            "</if> " +
            " ORDER BY create_time DESC " +
            "</script>"
    )
    List<EmailReminder> getEmailReminderByCorrelationId(
            @Param("mail_type") String mail_type,
            @Param("correlation_id") Integer correlation_id,
            @Param("create_time") String create_time
    );


    /* 批量插入（支持多记录一次性提交） */
    @Insert("<script>" +
            "INSERT INTO " + DB4TableName.EMAIL_REMINDER + " " +
            "(" +
            "mail_type, " +
            "mail_type_name, " +
            "user_name, " +
            "title, " +
            "mail_content_title, " +
            "mail_content, " +
            "template_code, " +
            "mail_address_list, " +
            "attachments, " +
            "mail_ad_groupList, " +
            "mail_department_list, " +
            "mail_group_paramList, " +
            "priority, " +
            "sendMail, " +
            "status, " +
            "error_info, "+
            "correlation_id, " +
            "creator_id, " +
            "create_time, " +
            "updater_id, " +
            "update_time " +
            ") " +
            "VALUES " +
            "<foreach collection='entities' item='entity' separator=','>" +
            "(" +
            "#{entity.mail_type}, " +
            "#{entity.mail_type_name}, " +
            "#{entity.user_name}, " +
            "#{entity.title}, " +
            "#{entity.mail_content_title}, " +
            "#{entity.mail_content}, " +
            "#{entity.template_code}, " +
            "#{entity.mail_address_list}, " +
            "#{entity.attachments}, " +
            "#{entity.mail_ad_groupList}, " +
            "#{entity.mail_department_list}, " +
            "#{entity.mail_group_paramList}, " +
            "#{entity.priority}, " +
            "#{entity.sendMail}, " +
            "#{entity.status}, " +
            "#{entity.error_info}, " +
            "#{entity.correlation_id}, " +
            "#{entity.creator_id}, " +
            "#{entity.create_time}, " +
            "#{entity.updater_id}, " +
            "#{entity.update_time} " +
            ")" +
            "</foreach>" +
            "</script>")
    Integer insertEmailReminderBatch(@Param("entities") List<EmailReminder> entities);

    /* 动态更新（按id主键） */
    @Update("<script>" +
            "UPDATE " + DB4TableName.EMAIL_REMINDER + " " +
            "<set>" +
            "  <if test='entity.mail_type != null'>mail_type = #{entity.mail_type},</if>" +
            "  <if test='entity.mail_type_name != null'>mail_type_name = #{entity.mail_type_name},</if>" +
            "  <if test='entity.user_name != null'>user_name = #{entity.user_name},</if>" +
            "  <if test='entity.title != null'>title = #{entity.title},</if>" +
            "  <if test='entity.mail_content_title != null'>mail_content_title = #{entity.mail_content_title},</if>" +
            "  <if test='entity.mail_content != null'>mail_content = #{entity.mail_content},</if>" +
            "  <if test='entity.template_code != null'>template_code = #{entity.template_code},</if>" +
            "  <if test='entity.mail_address_list != null'>mail_address_list = #{entity.mail_address_list},</if>" +
            "  <if test='entity.attachments != null'>attachments = #{entity.attachments},</if>" +
            "  <if test='entity.mail_ad_groupList != null'>mail_ad_groupList = #{entity.mail_ad_groupList},</if>" +
            "  <if test='entity.mail_department_list != null'>mail_department_list = #{entity.mail_department_list},</if>" +
            "  <if test='entity.mail_group_paramList != null'>mail_group_paramList = #{entity.mail_group_paramList},</if>" +
            "  <if test='entity.priority != null'>priority = #{entity.priority},</if>" +
            "  <if test='entity.sendMail != null'>sendMail = #{entity.sendMail},</if>" +
            "  <if test='entity.status != null'>status = #{entity.status},</if>" +
            "  <if test='entity.error_info != null'>error_info = #{entity.error_info},</if>" +
            "  <if test='entity.correlation_id != null'>correlation_id = #{entity.correlation_id},</if>" +
            "  <if test='entity.updater_id != null'>updater_id = #{entity.updater_id},</if>" +
            "  update_time = #{entity.update_time} " + // 强制更新时间
            "</set>" +
            "WHERE id = #{entity.id}" +
            "</script>")
    Integer updateEmailReminderById(@Param("entity") EmailReminder entity);

    /* 按id删除 */
    @Delete("<script>" +
            "DELETE FROM " + DB4TableName.EMAIL_REMINDER + " " +
            "WHERE id = #{id}" +
            "</script>")
    Integer deleteEmailReminderById(@Param("id") Integer id);

    /* 按id查询 */
    @Select("SELECT * FROM " + DB4TableName.EMAIL_REMINDER + " WHERE id = #{id}")
    EmailReminder getEmailReminderById(@Param("id") Integer id);



}
